package com.fankai.lease.web.app.controller.payment;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fankai.lease.common.result.Result;
import com.fankai.lease.model.entity.PaymentType;
import com.fankai.lease.model.entity.RoomPaymentType;
import com.fankai.lease.web.app.service.PaymentTypeService;
import com.fankai.lease.web.app.service.RoomPaymentTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "支付方式接口")
@RestController
@RequestMapping("/app/payment")
public class PaymentTypeController {
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Operation(summary = "根据房间id获取可选支付方式列表")
    @GetMapping("listByRoomId")
    public Result<List<PaymentType>> list(@RequestParam Long id) {
        List<PaymentType> paymentTypeList = null;
        LambdaQueryWrapper<RoomPaymentType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomPaymentType::getRoomId, id);
        List<RoomPaymentType> roomPaymentTypeList = roomPaymentTypeService.list();
        List<Long> payTypeList = roomPaymentTypeList.stream().map(RoomPaymentType::getId).toList();
        if (!payTypeList.isEmpty()) {
            LambdaQueryWrapper<PaymentType> paymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            paymentTypeLambdaQueryWrapper.in(PaymentType::getId, payTypeList);
            paymentTypeList = paymentTypeService.list();
        }
        return Result.ok(paymentTypeList);
    }

    @Operation(summary = "获取全部支付方式列表")
    @GetMapping("list")
    public Result<List<PaymentType>> list() {
        List<PaymentType> paymentTypeList = paymentTypeService.list();
        return Result.ok(paymentTypeList);
    }
}
